package com.kk.xhr.admin.controller;

import com.kk.xhr.admin.biz.IWorkOvertimeBizService;
import com.kk.xhr.admin.controller.request.ApplyQueryRequest;
import com.kk.xhr.admin.controller.request.WorkOvertimeAddRequest;
import com.kk.xhr.admin.controller.vo.WorkOvertimeVo;
import com.kk.xhr.common.pojo.ApplyStatusEnum;
import com.kk.xhr.web.util.ShiroUtil;
import com.kk.xhr.web.vo.ApiResult;
import com.kk.xhr.web.vo.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;

/**
 * 考勤管理控制器
 *
 * @author luokexiong
 * @version 1.0 2021/2/17
 * @since 1.0.0
 */
@RestController
@RequiredArgsConstructor
@Api(value = "加班管理", tags = "加班管理")
@RequestMapping("/overtime")
public class OvertimeController {
    private final Executor adminControllerBizExecutor;
    private final IWorkOvertimeBizService workOvertimeBizService;

    @ApiOperation(value = "查询我的加班申请", notes = "时间降序排序")
    @GetMapping
    public CompletionStage<ApiResult<PageVO<WorkOvertimeVo>>> queryMineOvertime(@Valid ApplyQueryRequest request) {
        Long userId = ShiroUtil.getAuthUserId();
        return CompletableFuture.supplyAsync(() ->
            ApiResult.success(workOvertimeBizService.queryPage(userId, request)), adminControllerBizExecutor);
    }

    @ApiOperation(value = "查询审核加班申请", notes = "时间降序排序")
    @GetMapping("/checkOvertime")
    public CompletionStage<ApiResult<PageVO<WorkOvertimeVo>>> queryCheckOvertime(@Valid ApplyQueryRequest request) {
        Long userId = ShiroUtil.getAuthUserId();
        return CompletableFuture.supplyAsync(() ->
            ApiResult.success(workOvertimeBizService.queryPageCheck(userId, request)), adminControllerBizExecutor);
    }

    @ApiOperation(value = "加班申请")
    @PostMapping
    public CompletionStage<ApiResult<Void>> overtime(@RequestBody @Valid WorkOvertimeAddRequest request) {
        Long userId = ShiroUtil.getAuthUserId();
        return CompletableFuture.supplyAsync(() -> {
            workOvertimeBizService.overtime(userId, request);
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }

    @ApiOperation(value = "修改加班状态")
    @PutMapping("/status/{id}")
    public CompletionStage<ApiResult<Void>> modifyStatus(@PathVariable("id") Long id, ApplyStatusEnum status) {
        return CompletableFuture.supplyAsync(() -> {
            workOvertimeBizService.modifyStatus(id, status);
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }
}
